#include "EndState.h"
#include "GameObjectSystem.h"
#include "physicsComponent.h"
#include "PositionControllerComponent.h"
#include "ScoreManager.h"
#include "gfxText.h"

EndState::EndState(const ObjectId& stateId)
	:FSMState(stateId)
	,m_uiWinner(NULL)
	,m_uiGameOver(NULL)
	,m_uiInfo(NULL)
{
	GameObjectSystem& gameObjectSystem = GameObjectSystem::GetSingleton();

	// Get the text properties.
	ObjectProperty* title1Property = gameObjectSystem.editProperty(GfxText::GFX_TEXT_ID, "title1");
	if (title1Property != NULL)
	{
		m_uiWinner = static_cast<GfxText*>(title1Property);
	}
	DLOG_ASSERT(m_uiWinner);

	ObjectProperty* title2Property = gameObjectSystem.editProperty(GfxText::GFX_TEXT_ID, "title2");
	if (title2Property != NULL)
	{
		m_uiGameOver = static_cast<GfxText*>(title2Property);
	}
	DLOG_ASSERT(m_uiGameOver);

	ObjectProperty* infoProperty = gameObjectSystem.editProperty(GfxText::GFX_TEXT_ID, "info1");
	if (infoProperty != NULL)
	{
		m_uiInfo = static_cast<GfxText*>(infoProperty);
	}
	DLOG_ASSERT(m_uiInfo);
}

void EndState::onEnter()
{
	// Set texts.
	if (ScoreManager::GetSingleton().player1Wins())
	{
		m_uiWinner->setText("* BLUE WINS! *");
		m_uiWinner->setFontColor(GfxColor::BLUE);
	}
	else
	{
		m_uiWinner->setText("* RED  WINS! *");
		m_uiWinner->setFontColor(GfxColor::RED);
	}
	m_uiWinner->setActive(true);

	m_uiGameOver->setText("* GAME OVER * ");
	m_uiGameOver->setFontColor(GfxColor::WHITE);
	m_uiGameOver->setActive(true);

	m_uiInfo->setText("Press 'R' to continue");
	m_uiInfo->setFontColor(GfxColor::WHITE);
	m_uiInfo->setActive(true);

	// Disable components (physics and input).
	GameObjectSystem& gameObjectSystem = GameObjectSystem::GetSingleton();
	
	Component* physicsComponent = gameObjectSystem.editComponent(PhysicsComponent::PHYSICS_COMPONENT_ID);
	physicsComponent->setActiveStatus(false);

	Component* positionControllerComponent = gameObjectSystem.editComponent(PositionControllerComponent::POSITION_CONTROLLER_ID);
	positionControllerComponent->setActiveStatus(false);

	ScoreManager::GetSingleton().reset();
}

void EndState::onLeave()
{
	// Disable texts.
	m_uiWinner->setActive(false);
	m_uiGameOver->setActive(false);
	m_uiInfo->setActive(false);

	// Enable components (physics and input).
	GameObjectSystem& gameObjectSystem = GameObjectSystem::GetSingleton();

	Component* physicsComponent = gameObjectSystem.editComponent(PhysicsComponent::PHYSICS_COMPONENT_ID);
	physicsComponent->setActiveStatus(true);

	Component* positionControllerComponent = gameObjectSystem.editComponent(PositionControllerComponent::POSITION_CONTROLLER_ID);
	positionControllerComponent->setActiveStatus(true);
}